﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace LabMS.Web.Equipment.common
{
    public partial class buyappperview : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("GMSQSH");//必须写在ispostback外
            strPageUserType.Add("mana");
            strPageUserType.Add("teacher");

            if (Request.QueryString["ID"] != null)
            {
                if (!IsPostBack)
                {
                    int ID;
                    if (int.TryParse(Request.QueryString["ID"].ToString(), out ID))
                    {
                        try
                        {
                            IDHF.Value = ID.ToString();
                            LabMS.BLL.BuyApp buyAppBLL = new LabMS.BLL.BuyApp();
                            LabMS.Model.BuyApp buyAppModel = new LabMS.Model.BuyApp();

                            buyAppModel = buyAppBLL.GetModel(ID);
                            AppCommentLbl.Text = LabMS.Common.codehelper.Decode(buyAppModel.AppComment);//申请备注
                            NumbersLbl.Text = buyAppModel.ClassNumber;//分类号码
                            NameLbl.Text = buyAppModel.Name;//仪器名称
                            NumLbl.Text = buyAppModel.Numbers;//购买数量
                            TypeDDL.SelectedValue = buyAppModel.Type;//类型
                            VerifyPerson.Text = buyAppModel.VerifyPerson;
                            VerifyTime.Text = buyAppModel.VerifyTime.ToString();//审核日期
                            lbVerifyComment.Text = LabMS.Common.codehelper.Decode(buyAppModel.VerifyComment);//审核备注
                            iniBtn(buyAppModel.State);
                        }
                        catch
                        {
                            ERRLbl.Text = "该购买申请不存在！";
                            ERRLbl.Visible = true;
                            AppBtn.Visible = false;
                            DelBtn.Visible = false;
                            mondifybtn.Visible = false;
                            ConAppBtn.Visible = false;
                            PassAppBtn.Visible = false;
                            UnPassAppBtn.Visible = false;
                        }
                    }
                    else
                    {
                        ERRLbl.Text = "系统异常，参数不合法！";
                        ERRLbl.Visible = true;
                        AppBtn.Visible = false;
                        DelBtn.Visible = false;
                        mondifybtn.Visible = false;
                        ConAppBtn.Visible = false;
                        PassAppBtn.Visible = false;
                        UnPassAppBtn.Visible = false;
                    }
                }
            }
            else
            {
                ERRLbl.Text = "系统异常，参数为空！";
                ERRLbl.Visible = true;
                AppBtn.Visible = false;
                DelBtn.Visible = false;
                mondifybtn.Visible = false;
                ConAppBtn.Visible = false;
                PassAppBtn.Visible = false;
                UnPassAppBtn.Visible = false;
            }
        }

        //申请
        protected void AppBtn_Click(object sender, EventArgs e)
        {
            LabMS.BLL.BuyApp buyAppBLL = new LabMS.BLL.BuyApp();
            LabMS.Model.BuyApp buyAppModel = new LabMS.Model.BuyApp();

            Button btn = (Button)sender;
            string strBtnID = btn.ID;
            string strAlert="";
            if (IDHF.Value != "")
            {
                int ID;
                
                if (int.TryParse(IDHF.Value, out ID))
                {
                    buyAppModel = buyAppBLL.GetModel(ID);
                    if (strBtnID == "AppBtn")
                    {
                        buyAppModel.State = "1";
                        strAlert = "申请成功，请等待审核!";
                    }
                    else if (strBtnID == "ConAppBtn")
                    {
                        buyAppModel.State = "0";
                        strAlert = "撤销申请成功!";
                    }
                    else if (strBtnID == "PassAppBtn")
                    {
                        buyAppModel.State = "3";
                        strAlert = "审核成功!";
                        buyAppModel.VerifyComment = LabMS.Common.codehelper.Encode(VerifyComment.Text.Trim());//审核备注
                        buyAppModel.VerifyTime = DateTime.Now;
                        buyAppModel.VerifyPerson=UserRealName;//审核人 Session获取
                    }
                    else if (strBtnID == "UnPassAppBtn")
                    {
                        buyAppModel.State = "2";
                        strAlert = "拒绝成功!";
                        buyAppModel.VerifyComment = LabMS.Common.codehelper.Encode(VerifyComment.Text.Trim());//审核备注
                        buyAppModel.VerifyTime = DateTime.Now;
                        //buyAppModel.VerifyPerson=;//审核人 Session获取
                    }

                    buyAppBLL.Update(buyAppModel);
                    LabMS.Common.JShelper.JSAlertAndRedirect(Page,"err",strAlert,"buyappperview.aspx?ID=" + ID);
                    return;
                }
            }

            LabMS.Common.JShelper.JSAlert(Page, "err", "操作失败!");
            return;
        }

        //删除
        protected void DelBtn_Click(object sender, EventArgs e)
        {
            LabMS.BLL.BuyApp buyAppBLL = new LabMS.BLL.BuyApp();

            if (IDHF.Value != "")
            {
                int ID;
                if (int.TryParse(IDHF.Value, out ID))
                {
                    buyAppBLL.Delete(ID);
                    LabMS.Common.JShelper.JSAlertAndRedirect(Page, "err", "删除成功!", "buyapplist.aspx");
                    return;
                }
            }

            LabMS.Common.JShelper.JSAlert(Page, "err", "删除失败!");
            return;
        }

        void iniBtn(string strStatus)
        {
            AppBtn.Visible = false;
            DelBtn.Visible = false;
            mondifybtn.Visible = false;
            ConAppBtn.Visible = false;
            PassAppBtn.Visible = false;
            UnPassAppBtn.Visible = false;

            if (strStatus == "0")
            {
                AppBtn.Visible = true;
                DelBtn.Visible = true;
                mondifybtn.Visible = true;
            }
            if (strStatus == "1")
            {
                ConAppBtn.Visible = true;
                PassAppBtn.Visible = true;
                UnPassAppBtn.Visible = true;
                paneltoCheck.Visible = true;
            }
            if (strStatus == "2")
            {
                mondifybtn.Visible = true;
                DelBtn.Visible = true;
                panelExamine.Visible = true;
            }
            if (strStatus == "3")
            {
                DelBtn.Visible = true;
                panelExamine.Visible = true;
            }
        }
    }
}
